﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Jabr.GUI.Forms;

namespace Jabr.Logic.Roster
{
    public class DisplayableAccount : TreeNode
    {
        AbstractAccount m_aa;

        public AbstractAccount Account
        {
            get
            {
                return m_aa;
            }
            set
            {
                m_aa = value;
                this.m_aa.OnPresence += delegate(GenericPresence pre)
                {
                    if (this.TreeView.InvokeRequired)
                        this.TreeView.Invoke(new invoke_Refresh_delegate(invoke_Refresh));
                    else
                        this.invoke_Refresh();
                };
            }
        }

        public DisplayableAccount(AbstractAccount aa)
        {
            this.Text = this.Name = aa.Username;
            this.Account = aa;
            this.Account.OnRoster += delegate(IEnumerable<DisplayableItem> roster)
            {
                RosterForm f = (RosterForm)this.TreeView.FindForm();
                if (f.InvokeRequired)
                    f.Invoke(new invoke_Roster_delegate(invoke_Roster), new object[] { roster, f });
                else
                    this.invoke_Roster(roster, f);
            };
        }

        private delegate void invoke_Refresh_delegate();
        private void invoke_Refresh()
        {
            this.TreeView.Refresh();
        }

        private delegate void invoke_Roster_delegate(IEnumerable<DisplayableItem> roster, RosterForm rf);
        private void invoke_Roster(IEnumerable<DisplayableItem> roster, RosterForm rf)
        {
            foreach (DisplayableItem di in roster)
            {
                if (di is DisplayableContact)
                {
                    var dc = (DisplayableContact)di;
                    dc.ContextMenuStrip = rf.CntItemMenu;
                    dc.PresenceChanged += delegate(GenericPresence presence, string status)
                    {
                        if (this.TreeView.InvokeRequired)
                            this.TreeView.Invoke(new invoke_Refresh_delegate(invoke_Refresh));
                        else
                            this.invoke_Refresh();
                    };
                    dc.PresenceProbe();
                }
                if (di is DisplayableGroup)
                {
                    foreach (DisplayableContact dc in di.Nodes)
                    {
                        dc.ContextMenuStrip = rf.CntItemMenu;
                        dc.PresenceChanged += delegate(GenericPresence presence, string status)
                        {
                            if (this.TreeView.InvokeRequired)
                                this.TreeView.Invoke(new invoke_Refresh_delegate(invoke_Refresh));
                            else
                                this.invoke_Refresh();
                        };
                        dc.PresenceProbe();
                    }
                    di.ContextMenuStrip = rf.GrpItemMenu;
                }
            }
            this.Nodes.AddRange(roster.ToArray());
        }
    }
}
